<script setup>
import {
    Goods,
    Tickets,
    Avatar,
    Management,
    UserFilled,
    User,
    Crop,
    EditPen,
    SwitchButton,
    CaretBottom,
    DeleteFilled
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'

//导入接口函数
import { userInfoGetService } from '@/api/user.js'
//导入pinia
import { useUserInfoStore } from '@/store/user.js'
const userInfoStore = useUserInfoStore();

import { ref } from 'vue'

import { useTokenStore } from '@/store/token.js';
const tokenStore = useTokenStore();

// 上传图片地址
import { imageStore } from '@/store/image.js';
const ImagetokenStore = imageStore();

//获取个人信息
const getUserInf = async () => {

    console.log('pId:' + tokenStore.token.id + '  role:' + userInfoStore.info.role)

    let result = await userInfoGetService(tokenStore.token.id);

    console.log(result)
    //存储pinia
    userInfoStore.setInfo(result.data)

}
getUserInf()

//dropDown条目被点击后，回调的函数
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { UserDelService} from "@/api/user";
const router = useRouter()
const handleCommand = (command) => {

    if (command === 'logout') {

        //退出登录
        // 提示用户  确认框
        ElMessageBox.confirm(
            '是否要退出登录？',
            '提示',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            }
        )
            .then(() => {
                //用户点击了确认
                //清空pinia中的token和个人信息
                userInfoStore.removeInfo()
                tokenStore.removeToken()
                ImagetokenStore.removeInfo()
                //  调用路由
                router.push('/')
                ElMessage({
                    type: 'success',
                    message: '退出登录',
                })
            }
            )
            .catch(() => {
                ElMessage({
                    type: 'info',
                    message: '取消',
                })
            })

    } else if (command === 'deluser') {
        // 用户注销操作
        // 弹出框 提醒 是否要注销用户
        ElMessageBox.confirm(
            '是否注销用户？',
            '提示',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            }
        )
            .then(() => {
                //用户点击了确认
                console.log('用户注销:')
                console.log(userInfoStore.info.id)
                UserDelService(userInfoStore.info.id)
                //清空pinia中的token和个人信息
                userInfoStore.removeInfo()
                tokenStore.removeToken()
                ImagetokenStore.removeInfo()
                
                //  调用路由
                router.push('/')
                ElMessage({
                    type: 'success',
                    message: '注销成功',
                })
            }
            )
            .catch(() => {
                ElMessage({
                    type: 'info',
                    message: '注销失败',
                })

            })
        // 注销用户操作
    }
    else {
        //路由
        router.push('/user/' + command)
    }
}

// const handlerouter = (command)=>{

//     if (command =='/Goodinfo' || '/user/manage') {
//         console.log('为什么没有执行')
//         if (userInfoStore.info.role === 1) {
//             ElMessage.error('权限不足！')
//             router.push('/Good')
//         }
//     }
// }@command="handlerouter"

</script>

<template>
    <el-container class="layout-container">
        <!-- 左侧菜单 -->
        <el-aside width="200px">
            <div class="el-aside__logo"></div>
            <el-menu  active-text-color="#ffd04b" background-color="#999" text-color="#fff" router>
                <el-menu-item index="/Good" command="/Good" >
                    <el-icon>
                        <Management />
                    </el-icon>
                    <span>商品管理</span>
                </el-menu-item>
                <el-menu-item index="/Goodinfo" command="/Goodinfo"  >
                    <el-icon>
                        <Management />
                    </el-icon>
                    <span>商品信息管理</span>
                </el-menu-item>
                <el-menu-item index="/user/manage" command="/user/manage">
                    <el-icon>
                        <Avatar />
                    </el-icon>
                    <span>人员管理</span>
                </el-menu-item>
                <el-menu-item index="/list" command="/list">
                    <el-icon>
                        <Tickets />
                    </el-icon>
                    <span>订单管理</span>
                </el-menu-item>
                <el-menu-item index="/shoppingcart" command="/shoppingcart" >
                    <el-icon>
                        <Goods />
                    </el-icon>
                    <span>购物车管理</span>
                </el-menu-item>
                <el-sub-menu index="">
                    <template #title>
                        <el-icon>
                            <UserFilled />
                        </el-icon>
                        <span>个人中心</span>
                    </template>

                    <el-menu-item index="/user/info">
                        <el-icon>
                            <User />
                        </el-icon>
                        <span>基本资料</span>
                    </el-menu-item>
                    <el-menu-item index="/user/mySubmit">
                        <el-icon>
                            <Tickets />
                        </el-icon>
                        <span>我的商品提交历史</span>
                    </el-menu-item>
                    <!-- <el-menu-item index="/user/avatar">
                        <el-icon>
                            <Crop />
                        </el-icon>
                        <span>更换头像</span>
                    </el-menu-item> -->
                    <el-menu-item index="/user/resetpass">
                        <el-icon>
                            <EditPen />
                        </el-icon>
                        <span>其他</span>
                    </el-menu-item>
                </el-sub-menu>
            </el-menu>
        </el-aside>
        <!-- 右侧主区域 -->
        <el-container>
            <!-- 头部区域 -->
            <el-header>
                <div>用户：<strong>{{ userInfoStore.info.username ? userInfoStore.info.username :
                    userInfoStore.info.usrename
                        }}</strong></div>
                <!-- 下拉菜单 -->
                <el-dropdown @command="handleCommand" placement="bottom-end">
                    <span class="el-dropdown__box">
                        <el-avatar :src="userInfoStore.info.image ? userInfoStore.info.image : avatar" />
                        <el-icon>
                            <CaretBottom />
                        </el-icon>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <!-- command中的参数是路由名称 -->
                            <el-dropdown-item command="info" :icon="User">基本资料</el-dropdown-item>
                            <el-dropdown-item command="info" :icon="Crop">更换头像</el-dropdown-item>
                            <el-dropdown-item command="resetpass" :icon="EditPen">重置密码</el-dropdown-item>
                            <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
                            <el-dropdown-item command="deluser" :icon="DeleteFilled">账号注销</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>
            <!-- 中间区域 -->
            <el-main>
                <!-- <div style="width: 1290px; height: 570px;border: 1px solid red;">
                    内容展示区
                </div> -->

                <RouterView></RouterView>
            </el-main>
            <!-- 底部区域 -->
            <el-footer>jinju ©2024 Created by XXX</el-footer>
        </el-container>
    </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
    height: 100vh;

    .el-aside {
        background-color: #999;

        &__logo {
            height: 120px;
            background: url('@/assets/logo3.png') no-repeat center / 120px auto;
        }

        .el-menu {
            border-right: none;
        }
    }

    .el-header {
        background-color: #fff;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .el-dropdown__box {
            display: flex;
            align-items: center;

            .el-icon {
                color: #999;
                margin-left: 10px;
            }

            &:active,
            &:focus {
                outline: none;
            }
        }
    }

    .el-footer {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        color: #666;
    }
}
</style>
